package com.qf.controller;

import com.qf.service.AdminService;
import com.qf.service.impl.AdminServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 管理员登录
 * @author 千锋健哥
 */
@WebServlet("/admin")
public class AdminController extends BaseController{

    /**
     * 管理员登录
     * @param request
     * @param response
     * @return
     */
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //1. 接收页面参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username == null || password == null) {
            return "redirect:/admin/login.jsp";
        }

        //2. 根据页面参数, 调用service, 校验用户名, 密码
        AdminService adminService = new AdminServiceImpl();
        boolean flag = adminService.login(username, password);


        if (flag) {
            //3. 如果登录成功, 那么将用户名放入session中保存, 跳转到首页
            request.getSession().setAttribute("admin", username);
            return "redirect:/admin/admin.jsp";
        } else {
            //4. 如果登录不成功跳转到登录页面, 从新让用户输入用户名, 密码
            return "redirect:/admin/login.jsp";
        }



    }
}
